<?php

namespace app\index\controller\session;

use ShidianUtil\Thinkphp\Response\ApiResponse;
use think\exception\ValidateException;
use think\Request;

/**
 * 微信小程序 & 抖音小程序 登录
 * Class WechatMiniprogram
 * @package app\session\controller
 */
class Miniprogram
{
    private Request $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * 三方登录
     * @return ApiResponse
     * @url /session/miniprogram/save
     */
    public function save(): ApiResponse
    {

        $pf = $this->request->header('pf', PT_WX);

        if ($pf == PT_WX) {

            return $this->getWechatMiniprogram()->save();
        } elseif ($pf == PT_TT) {

            return $this->getDouyinMiniprogram()->save();
        }

        throw new ValidateException('不支持该登录方式');
    }

    private function getWechatMiniprogram(): WechatMiniprogram
    {
        return app(WechatMiniprogram::class);
    }

    private function getDouyinMiniprogram(): DouyinMiniprogram
    {
        return app(DouyinMiniprogram::class);
    }

}
